package com.njitt.bysj.srevice;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.njitt.bysj.base.BaseService;
import com.njitt.bysj.bean.*;
import com.njitt.bysj.mapper.*;
import com.njitt.bysj.query.HistoryQuery;
import com.njitt.bysj.query.QualityQuery;
import com.njitt.bysj.utils.AssertUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @description:
 * @author: wyj
 * @time: 2022/4/19 16:49
 */
@Service
public class ProjectQualityService extends BaseService<ProjectQualityBean, Integer> {

    @Resource
    private PurchaseRequestBeanMapper purchaseRequestBeanMapper;

    @Resource
    private ProjectQualityBeanMapper projectQualityBeanMapper;

    @Resource
    private QualityHistoryBeanMapper qualityHistoryBeanMapper;

    @Resource
    private SupplierService supplierService;

    @Resource
    private WarehouseBeanService warehouseBeanService;

    @Resource
    private ProjectService projectService;

    @Resource
    private ProjectPurchaseBeanMapper projectPurchaseBeanMapper;

    @Resource
    private InventoryBeanMapper inventoryBeanMapper;

    public Map<String, Object> selectByParamsWithPurchase(QualityQuery query) {
        PageHelper.startPage(query.getPage(), query.getLimit());
        Map<String, Object> map = new HashMap<>();
        PageInfo<ProjectQualityBean> info = new PageInfo<>(projectQualityBeanMapper.selectByParamsWithPurchase(query));
        map.put("code", 0);
        map.put("data", info.getList());
        map.put("msg", "success");
        map.put("count", info.getTotal());
        return map;
    }

    @SuppressWarnings("AlibabaTransactionMustHaveRollback")
    @Transactional(propagation = Propagation.REQUIRED)
    public void arrivalPurchaseRequestOrder(List<PurchaseRequestBean> requestBeanList) {
        /**
         * @description: 到货转质检单
         * @return:  * @param requestBeanList
         * @author:
         * @time: 2022/4/19 17:50
         */
//        QualityQuery qualityQuery = new QualityQuery();
//        List<ProjectQualityBean> projectQualityBeans= projectQualityBeanMapper.selectByParamsWithPurchase(qualityQuery);
//        projectQualityBeans.forEach(projectQualityBean->{
//            for (List<PurchaseRequestBean> requestBeans: requestBeanList) {
//
//            }
//        });


        AssertUtil.isTrue(requestBeanList.size() == 0, "到货失败");
        List<ProjectQualityBean> projectQualityBeanList = new ArrayList<>();
        requestBeanList.forEach(r -> {
            ProjectQualityBean projectQualityBean = new ProjectQualityBean();
            projectQualityBean.setQualityType(0);
            projectQualityBean.setPurchaseId(r.getId());
            projectQualityBean.setQualityCreateDate(new Date());
            projectQualityBean.setQualityUpdateDate(new Date());
            projectQualityBean.setQualityStatus(0);
            projectQualityBeanList.add(projectQualityBean);
        });

        Integer integer = projectQualityBeanMapper.insertBatch(projectQualityBeanList);
        AssertUtil.isTrue(integer != projectQualityBeanList.size(), "转质检失败");
        requestBeanList.forEach(requestBean -> {
            requestBean.setOrderStatus(2);
        });
        Integer integer1 = purchaseRequestBeanMapper.updateBatch(requestBeanList);
        AssertUtil.isTrue(integer1 != 1, "更新失败");

    }

    @SuppressWarnings("AlibabaTransactionMustHaveRollback")
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateEmpToQuality(ProjectQualityBean qualityBean) {
        /**
         * @description: 指派质检员
         * @return:  * @param qualityBean
         * @author:
         * @time: 2022/4/19 21:38
         */
        ProjectQualityBean projectQualityBean = projectQualityBeanMapper.selectByPrimaryKey(qualityBean.getId());
        AssertUtil.isTrue(projectQualityBean == null, "指派失败1");
        AssertUtil.isTrue(qualityBean.getQualityerId().equals(projectQualityBean.getQualityerId()), "重复指派");
        AssertUtil.isTrue(qualityBean.getId() == null || qualityBean.getQualityerId() == 0, "指派错误");
        AssertUtil.isTrue(qualityBean.getQualityerId() == null || qualityBean.getQualityerId() == 0, "指派错误2");
        qualityBean.setQualityStatus(1);
        Integer integer = projectQualityBeanMapper.updateByPrimaryKeySelective(qualityBean);
        AssertUtil.isTrue(integer == 0 || integer == null, "指派失败3");
    }

    @SuppressWarnings("AlibabaTransactionMustHaveRollback")
    @Transactional(propagation = Propagation.REQUIRED)
    public void addQualityResult(QualityHistoryBean qualityHistoryBean) {
        /**
         * @description:采购质检
         * @return:  * @param qualityHistoryBean
         * @author:
         * @time: 2022/4/20 9:58
         */
        AssertUtil.isTrue(qualityHistoryBean.getQualityResult() == null, "质检结果不能不为空");
        if (qualityHistoryBean.getQualityResult() == 0) {
            /**
             * @description: 质检同意
             * @return:  * @param qualityHistoryBean
             * @author:
             * @time: 2022/4/20 12:38
             */
            qualityResultIsApprove(qualityHistoryBean);
        }
        if (qualityHistoryBean.getQualityResult() == 1) {
            qualityResultIsNotApprove(qualityHistoryBean);
        }


    }

    private void qualityResultIsNotApprove(QualityHistoryBean qualityHistoryBean) {
        /**
         * @description: 质检 不同意
         * @return:  * @param qualityHistoryBean
         * @author:
         * @time: 2022/4/20 12:38
         */

        boolean approve = false;
        AssertUtil.isTrue(qualityHistoryBean.getPurchaseId() == null, "添加失败");
        AssertUtil.isTrue(qualityHistoryBean.getQualityResult() == null, "添加失败");
        qualityHistoryBean.setQualityNo(System.currentTimeMillis() + "");
        qualityHistoryBean.setQualityData(new Date());
        qualityHistoryBean.setQualityStatus(0);
        qualityHistoryBeanMapper.insertSelective(qualityHistoryBean);
        // 采购订单状态
        supplierService.updatePurchaseOrderStatus(approve, qualityHistoryBean.getPurchaseId());
        // 质检单 移除 不同意订单
        ProjectQualityBean bean = new ProjectQualityBean();
        bean.setQualityStatus(2);
        bean.setId(qualityHistoryBean.getQualityId());
        System.out.println(qualityHistoryBean.getQualityId());
        Integer integer = projectQualityBeanMapper.updateByPrimaryKeySelective(bean);
        AssertUtil.isTrue(integer == null|| integer == 0, "修改失败");
    }

    private void qualityResultIsApprove(QualityHistoryBean qualityHistoryBean) {
        /**
         * @description: 质检结果同意 采购入库 库存变化 采购订单结束
         * @return:  * @param qualityHistoryBean
         * @author:
         * @time: 2022/4/20 10:06
         */
        boolean approve = true;
        AssertUtil.isTrue(qualityHistoryBean.getPurchaseId() == null || qualityHistoryBean.getPurchaseId() == 0, "添加失败");
        AssertUtil.isTrue(qualityHistoryBean.getQualityerId() == null, "质检结果不能为空");
        qualityHistoryBean.setQualityNo(System.currentTimeMillis() + "");
        qualityHistoryBean.setQualityData(new Date());
        qualityHistoryBean.setQualityStatus(0);
        qualityHistoryBeanMapper.insertSelective(qualityHistoryBean);
        // 修改采购的订单状态
        supplierService.updatePurchaseOrderStatus(approve, qualityHistoryBean.getPurchaseId());
        // 库存入库
        warehouseBeanService.purchaseToWareHouse(qualityHistoryBean.getPurchaseId());
        // 全部质检完成后修改质检单状态
        Integer status = 1;
        projectQualityBeanMapper.updateStatus(qualityHistoryBean.getPurchaseId(), status);
    }

    public Map<String, Object> selectByParamsWithQualityHistory(HistoryQuery query) {
        /**
         * @description: 质检单入库历史
         * @return:  * @param query
         * @author:
         * @time: 2022/4/20 11:28
         */
        PageHelper.startPage(query.getPage(), query.getLimit());
        Map<String, Object> map = new HashMap<>(16);
        PageInfo<QualityHistoryBean> info = new PageInfo<>(qualityHistoryBeanMapper.selectByParamsWithQualityHistory(query));
        map.put("code", 0);
        map.put("data", info.getList());
        map.put("msg", "success");
        map.put("count", info.getTotal());
        return map;
    }

    public Map<String, Object> selectByParamsWithEntryHistoryHistory(HistoryQuery query) {
        PageHelper.startPage(query.getPage(), query.getLimit());
        Map<String, Object> map = new HashMap<>(16);
        PageInfo<QualityHistoryBean> info = new PageInfo<>(qualityHistoryBeanMapper.selectByParamsWithEntryHistory(query));
        map.put("data", info.getList());
        map.put("code", 0);
        map.put("count", info.getTotal());
        map.put("msg", "success");
        return map;
    }

    public Map<String, Object> selectByParamsWithProduce(QualityQuery query) {
        /**
         * @description: 生产质检列表
         * @return:  * @param query
         * @author:
         * @time: 2022/4/23 0:57
         */
        PageHelper.startPage(query.getPage(), query.getLimit());
        Map<String, Object> map = new HashMap<>(16);
        PageInfo<ProjectQualityBean> info = new PageInfo<>(projectQualityBeanMapper.selectByParamsWithProduce(query));
        map.put("data", info.getList());
        map.put("code", 0);
        map.put("msg", "success");
        map.put("count", info.getTotal());
        return map;
    }

    @SuppressWarnings("AlibabaTransactionMustHaveRollback")
    @Transactional(propagation = Propagation.REQUIRED)
    public void addEmpToProduceQuality(ProjectQualityBean qualityBean) {
        /**
         * @description: 产成品质检
         * @return:  * @param qualityBean
         * @author:
         * @time: 2022/4/25 11:41
         */
        AssertUtil.isTrue(qualityBean.getId() == null, "添加失败");
        AssertUtil.isTrue(qualityBean.getQualityerId() == null, "质检人员添加失败");
        AssertUtil.isTrue(qualityBean.getProjectId() == null, "项目添加失败");
        AssertUtil.isTrue(qualityBean.getProjectId() == null, "项目添加失败");
        qualityBean.setQualityStatus(1);
        Integer integer = projectQualityBeanMapper.updateByPrimaryKeySelective(qualityBean);
        AssertUtil.isTrue(integer == null || integer == 0, "添加失败");
    }


    @SuppressWarnings("AlibabaTransactionMustHaveRollback")
    @Transactional(propagation = Propagation.REQUIRED)
    public void addProduceProjectQualityResult(QualityHistoryBean qualityHistoryBean) {
        /**
         * @description: 产成品质检
         * @return:  * @param qualityHistoryBean
         * @author:
         * @time: 2022/4/24 14:22
         */
        if (qualityHistoryBean.getQualityResult() == 0) {
            addApproveProduceResult(qualityHistoryBean);
        } else if (qualityHistoryBean.getQualityResult() == 1) {
            addNoApproveProduceResult(qualityHistoryBean);
        }
    }

    private void addApproveProduceResult(QualityHistoryBean qualityHistoryBean) {
        System.out.println(qualityHistoryBean);
        // qualityId == 11
        //质检同意入库
        // 修改生产质检单状态
        ProjectQualityBean projectQualityBean = new ProjectQualityBean();
        projectQualityBean.setQualityStatus(2);
        projectQualityBean.setId(qualityHistoryBean.getQualityId());
        Integer integer4 = projectQualityBeanMapper.updateByPrimaryKeySelective(projectQualityBean);
        AssertUtil.isTrue(integer4 == null || integer4 == 0, "质检失败");


        AssertUtil.isTrue(qualityHistoryBean.getProjectId() == null || qualityHistoryBean.getProjectId() == 0, "质检失败");
        AssertUtil.isTrue(qualityHistoryBean.getQualityerId() == null || qualityHistoryBean.getQualityerId() == 0, "质检人员不存在");
        qualityHistoryBean.setQualityStatus(0);
        qualityHistoryBean.setQualityData(new Date());
        qualityHistoryBean.setQualityNo(""+System.currentTimeMillis());
        Integer integer3 = qualityHistoryBeanMapper.insertSelective(qualityHistoryBean);
        AssertUtil.isTrue(integer3 == null || integer3 == 0, "质检失败");

        //修改项目状态
        ProjectBean projectBean = projectService.selectByPrimaryKey(qualityHistoryBean.getProjectId()) ;
        projectBean.setStatus(3);

        // 修改采购状态
        ProjectPurchaseBean projectPurchaseBean = projectPurchaseBeanMapper.selectByProjectId(qualityHistoryBean.getProjectId());
        AssertUtil.isTrue(projectPurchaseBean == null, "质检失败");
        projectPurchaseBean.setStatus(3);
        Integer integer1 = projectPurchaseBeanMapper.updateByPrimaryKeySelective(projectPurchaseBean);
        AssertUtil.isTrue(integer1 == null || integer1 == 0, "质检失败");
        // 产成品入库
        InventoryBean inventoryBean = new InventoryBean();
        inventoryBean.setProjectId(qualityHistoryBean.getProjectId());
        inventoryBean.setOutStatus(0);
        int stockCount = projectBean.getBasicQuotationBean().getProductCount().intValue();
        inventoryBean.setStockCount(stockCount);
        inventoryBean.setOutCount(0);
        Integer integer2 = inventoryBeanMapper.insertSelective(inventoryBean);
        AssertUtil.isTrue(integer2 == null || integer2 == 0, "质检失败");
        // 修改项目状态
        Integer integer = projectService.updateByPrimaryKeySelective(projectBean);
        AssertUtil.isTrue(integer == null || integer == 0, "质检失败");
        // 修改质检状态

    }

    private void addNoApproveProduceResult(QualityHistoryBean qualityHistoryBean) {
        AssertUtil.isTrue(qualityHistoryBean.getProjectId() == null, "质检失败");
        AssertUtil.isTrue(qualityHistoryBean.getQualityResult() == null, "质检失败2");
        ProjectBean bean = projectService.selectProjectByProjectId(qualityHistoryBean.getProjectId());
        qualityHistoryBean.setQualityNo(System.currentTimeMillis() + "");
        AssertUtil.isTrue(bean == null, "质检项目不存在");

        // 修改生产质检单状态
        ProjectQualityBean projectQualityBean = new ProjectQualityBean();
        projectQualityBean.setQualityStatus(2);
        projectQualityBean.setId(qualityHistoryBean.getQualityId());
        Integer integer2 = projectQualityBeanMapper.updateByPrimaryKeySelective(projectQualityBean);
        AssertUtil.isTrue(integer2 == null || integer2 == 0, "质检失败");
        // 添加质检历史单
        qualityHistoryBean.setQualityData(new Date());
        qualityHistoryBean.setQualityStatus(0);
        Integer integer1 = qualityHistoryBeanMapper.insertSelective(qualityHistoryBean);
        AssertUtil.isTrue(integer1 == null || integer1 == 0, "质检失败");
        // 修改生产订单状态
        bean.setStatus(1);
        Integer integer = projectService.updateByPrimaryKeySelective(bean);
        AssertUtil.isTrue(integer == null || integer == 0, "添加质检结果失败");
    }

    public Map<String, Object> selectByParamsWithProduceQualityHistory(HistoryQuery query) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(query.getPage(), query.getLimit());
        PageInfo<QualityHistoryBean> info = new PageInfo<>(qualityHistoryBeanMapper.selectByParamsWithProduceQualityHistory(query));
        map.put("code", 0);
        map.put("data", info.getList());
        map.put("msg", "success");
        map.put("count", info.getTotal());
        return map;
    }
}
